<?php
/**
* Контроллер главной страницы сайта
*
*/
class IndexController extends Zend_Controller_Action
{
	/**
	 * 
	 * @var Zend_Config_Ini
	 */
	private $config;
	private $nameSpace;
	private $key;
	/**
	 * Соединенеи
	 * @var Zend_Db_Adapter_Abstract
	 */
	private $db;
	function init()
	{
		$this->db = Zend_Registry::get('db');
		$this->nameSpace = '/'.$this->getRequest()->getModuleName().'/'.$this->getRequest()->getControllerName(); 
		$this->key = '/'.$this->getRequest()->getModuleName().'/'.$this->getRequest()->getControllerName().'/'.$this->getRequest()->getActionName();
		$this->config = Zend_Registry::get('config');
		$pageConfig = $this->config->toArray();
		$this->view->title = $pageConfig[$this->key]['title'];
		$this->view->description = $pageConfig[$this->key]['description'];
		$this->view->keywords = $pageConfig[$this->key]['keywords'];
		$this->view->h1 = $pageConfig[$this->key]['h1'];
		$this->view->footerdesc = $pageConfig[$this->key]['footerdesc'];
		$this->view->footerdesc2 = $pageConfig[$this->key]['footerdesc2'];
		$this->view->footerdesc3 = $pageConfig[$this->key]['footerdesc3'];
		$this->view->addScriptPath('./application/views/scripts/'.$this->getRequest()->getControllerName());
	}

	/**
	* Действие по умолчанию (/index)
	*
	*/
	function indexAction()
	{
		//$this->view->title = '';
	}
	function loginformAction()
	{
		
	}
	function loginAction()
	{
		$f = new Zend_Filter_StripTags();
		$username = $f->filter($this->_request->__get('username'));
		$password = $f->filter($this->_request->__get('pass'));
		$authAdapter = new Zend_Auth_Adapter_DbTable($this->db);
		$authAdapter->setTableName('cms_user');
		$authAdapter->setIdentityColumn('name');
		$authAdapter->setCredentialColumn('pass');
			// Set the input credential values to authenticate against
		$authAdapter->setIdentity($username);
		$authAdapter->setCredential($password);
			//do the authentication
		$auth = Zend_Auth::getInstance();
				
		$result = $auth->authenticate($authAdapter);
		if ($result->isValid()) {
			$data = $authAdapter->getResultRowObject(null, 'pass');
			$data->role = 'admin';//$role->getNameById($data->cms_role_id);
  			$auth->getStorage()->write($data);
			$this->_redirect('/cms');
		}
		else{
			Zend_Auth::getInstance()->clearIdentity();
			print "not valid password";
			//$this->_redirect('/index/loginform');
		}	
	}
	function logoutAction()
	{
		Zend_Auth::getInstance()->clearIdentity();
		$this->_forward('index');
	}
}